package com.genesis.yunnanji.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Kite
 * @date: 2018-08-22
 * @description:
 */
public class BasePagerFragmentAdapter extends FragmentPagerAdapter {
    private List<Fragment> mFragments;

    public BasePagerFragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
        super(fragmentManager);
        this.mFragments = fragments;
    }

    public int getCount() {
        return this.mFragments == null ? 0 : this.mFragments.size();
    }

    public Fragment getItem(int position) {
        return this.mFragments == null ? null : (Fragment)this.mFragments.get(position);
    }

    public void setDatas(List<Fragment> datas) {
        this.mFragments = datas;
        this.notifyDataSetChanged();
    }

    public void addDatas(List<Fragment> datas) {
        if (this.mFragments == null) {
            this.mFragments = datas;
        } else {
            this.mFragments.addAll(datas);
        }

        this.notifyDataSetChanged();
    }

    public void addData(int position, Fragment model) {
        if (this.mFragments == null) {
            this.mFragments = new ArrayList();
        }

        this.mFragments.add(position, model);
        this.notifyDataSetChanged();
    }

    public void clearDatas() {
        if (this.mFragments != null) {
            this.mFragments.clear();
        }

        this.mFragments = null;
    }

    public void setItem(Fragment oldModel, Fragment newModel) {
        this.setItem(this.mFragments.indexOf(oldModel), newModel);
    }

    public void setItem(int location, Fragment newModel) {
        this.mFragments.set(location, newModel);
        this.notifyDataSetChanged();
    }

    public void removeItem(Fragment model) {
        this.removeItem(this.mFragments.indexOf(model));
    }

    public void removeItem(int position) {
        this.mFragments.remove(position);
        this.notifyDataSetChanged();
    }
}
